package com.yuyang.wifi.views.recyclerview.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by tik on 17/8/11.
 */

public class CommonViewHolder extends RecyclerView.ViewHolder{
	private SparseArray<View> views;
	private Context mContext;
	public CommonViewHolder(Context context, View itemView) {
		super(itemView);
		mContext = context;
		views = new SparseArray<>();
	}


	public static CommonViewHolder get(Context context, ViewGroup viewGroup, int layoutId) {
		View itemView = LayoutInflater.from(context).inflate(layoutId, viewGroup, false);
		return new CommonViewHolder(context, itemView);
	}

	public static CommonViewHolder get(Context context, int layoutId) {
		View itemView = LayoutInflater.from(context).inflate(layoutId, null);
		return new CommonViewHolder(context, itemView);
	}

	public static CommonViewHolder createViewHolder(Context context, View itemView) {
		CommonViewHolder holder = new CommonViewHolder(context, itemView);
		return holder;
	}

	public <T extends View> T getView(int viewId){
		return getView(null,viewId);
	}

	public <T extends View> T getView(Class<T> clazz, int viewId){
		View view = views.get(viewId);
		if (view == null) {
			view = itemView.findViewById(viewId);
			views.put(viewId, view);
		}
		return (T) view;
	}

	public CommonViewHolder setText(int viewId, String text) {
		getView(TextView.class,viewId).setText(text);
		return this;
	}

	public String getText(int viewId) {
		return getView(TextView.class,viewId).getText().toString();
	}

	public CommonViewHolder setTextColor(int viewId, int color) {
		getView(TextView.class,viewId).setTextColor(color);
		return this;
	}

	public CommonViewHolder setImageResource(int viewId, int resId) {
		getView(ImageView.class,viewId).setImageResource(resId);
		return this;
	}


	public CommonViewHolder setViewVisible(int viewId, int visibility) {
		getView(viewId).setVisibility(visibility);
		return this;
	}

	public CommonViewHolder setViewBackgroundResource(int viewId, int resId) {
		getView(viewId).setBackgroundResource(resId);
		return this;
	}

	public CommonViewHolder setOnclickListener(int viewId, View.OnClickListener listener) {
		getView(viewId).setOnClickListener(listener);
		return this;
	}

}
